/*
 * Created by Lettle on 2021/8/14.
 * QQ: 1071445082
 * github: https://github.com/Python-Lettle
 * gitee : https://gitee.com/lettle/
 */

#include "cocktail.h"

FILE *fp = NULL;

// 编译器帮助信息
static void usage(int exitcode) {
    fprintf(exitcode ? stderr : stdout,
            "Usage: cocotail [ -E ][ -a ] [ -h ] <file>\n\n"
            "\n"
            "  -I<path>          add to include path\n"
            "  -E                print preprocessed source code\n"
            "  -D name           Predefine name as a macro\n"
            "  -D name=def\n"
            "  -S                Stop before assembly (default)\n"
            "  -c                Do not run linker (default)\n"
            "  -U name           Undefine name\n"
            "  -fdump-ast        print AST\n"
            "  -fdump-stack      Print stacktrace\n"
            "  -fno-dump-source  Do not emit source code as assembly comment\n"
            "  -o filename       Output to the specified file\n"
            "  -g                Do nothing at this moment\n"
            "  -Wall             Enable all warnings\n"
            "  -Werror           Make all warnings into errors\n"
            "  -O<number>        Does nothing at this moment\n"
            "  -m64              Output 64-bit code (default)\n"
            "  -w                Disable all warnings\n"
            "  -h                print this help\n"
            "\n"
            "One of -a, -c, -E or -S must be specified.\n\n");
    exit(exitcode);
}



int main(int argc, char *argv[]) {

    struct cot_token_stream cotTokenStream = {0, 0, NULL};
    cotTokenStream.capacity = 8;
    cotTokenStream.count = 0;
    cotTokenStream.tokens = (cot_token *) malloc(8 * sizeof(cot_token));
    printf("Created a size %d for total %d\n",cotTokenStream.capacity, 8*sizeof(cot_token));
    /** 分析传入的参数 **/
    if(argc == 1)           // 什么也没有传入
        usage(1);
    else
    for (int i=1;i < argc;i++){
        if(!strcmp(argv[i],"-h") || !strcmp(argv[i], "--help"))
            usage(0);
        else {
            fp = fopen(argv[i], "r");                        // 用文件名尝试打开

            // 打开文件失败
            if (!fp) {
                printf("Please check your paramer.\n");
                usage(1);                                   // 显示帮助信息 并结束程序
            }

        }
    }

    // 打开文件成功
    cot_token_scan(fp, &cotTokenStream);

    // 调试模式下输出 token 流
    if(__DEBUG_MODE__) {
        for(int i=0; i<cotTokenStream.count; i++) {
            switch (cotTokenStream.tokens[i].value->type) {
                case FLOAT_LITERAL:
                    printf("%lf ", cotTokenStream.tokens[i].value->float_value);
                    break;
                case INTEGER_LITERAL:
                    printf("%d ", cotTokenStream.tokens[i].value->int_value);
                    break;
                default:
                    printf("%s ", cotTokenStream.tokens[i].value->string_value);
                    break;
            }
        }
    }
    printf("\n");


    fclose(fp);
    return 0;
}


